package com.hnkywl.sts.dao.jsgl;

import com.hnkywl.sts.entity.cggl.Cgjsdmx;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.type.Type;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by Jerry on 13-10-15.
 */
@Repository
public class CgjsdmxDao extends SimpleHibernateTemplate<Cgjsdmx, Long> {
    public List<Map> findCgjsdmxSearch(Long cgjsdId) {
        Criteria criteria = getSession().createCriteria(this.entityClass)
                .createAlias("gg", "gg", CriteriaSpecification.LEFT_JOIN)
                .createAlias("cd", "cd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("jsd", "cgjsd", CriteriaSpecification.LEFT_JOIN)
                .createAlias("cgd", "cgd", CriteriaSpecification.LEFT_JOIN)
                .setProjection(Projections.projectionList()
                        .add(Property.forName("id").as("id"))
                        .add(Property.forName("cgd.id").as("cgd.id"))
                        .add(Property.forName("cgd.cgdh").as("cgdh"))
                        .add(Property.forName("gg.id").as("wzgg.id"))
                        .add(Property.forName("gg.code").as("wzgg.code"))
                        .add(Property.forName("gg.name").as("wzgg.name"))
                        .add(Projections.sqlProjection("gg1_.ggxh as \"wzgg.ggxh\"", new String[]{"wzgg.ggxh"}, new Type[]{Hibernate.STRING}))
                        .add(Property.forName("gg.dw").as("wzgg.dw"))
                        .add(Property.forName("gg.cz").as("wzgg.cz"))
                        .add(Property.forName("ck").as("ck"))
                        .add(Property.forName("cd.id").as("cd.id"))
                        .add(Property.forName("cd.z").as("cd.z"))
                        .add(Property.forName("rksl").as("rksl"))
                        .add(Property.forName("hsj").as("hsj"))
                        .add(Property.forName("sjdj").as("sjdj"))
                        .add(Property.forName("bhsj").as("bhsj"))
                        .add(Property.forName("je").as("je"))
                        .add(Property.forName("se").as("se"))
                        .add(Property.forName("ce").as("ce"))
                        .add(Property.forName("cgjsd.me").as("me"))
                        .add(Property.forName("cgjsd.ddfy").as("ddfy"))
                ).add(Restrictions.eq("cgjsd.id", cgjsdId))
                .setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        criteria.addOrder(Property.forName("gg.code").asc());
        return criteria.list();
    }
}
